import { config} from '../../../shared/smartadmin.config';
import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
declare var $: any;
@Component({
  selector: 'sa-rail-count',
  templateUrl: './rail-count.component.html',
})
export class RailCountComponent implements OnInit {
	tableData:any= []; //table数据
	totalCount = 0; //分页总条数
	curPage = 1; //分页当前页
	pageSize = 10; //内置的分页每页条数
	searchKey = ''; //搜索keyword
	companyId = '';
	startTime = '';//开始时间
	endTime = '';//结束时间
	selectedCompanyId = '';
	selectedCompanyName = '';
	railName='';//围栏名称
	siteType='1';//站点类别
	taggingName='1';//标注名称
	nowCarlist:any=[]; //记录查看的在场车辆
	constructor(private jsonApiService: JsonApiService,private router: Router) {}
	//刷新
	refresh(){
		this.load();
	}
	ngOnInit() {
		this.initDate().then(()=>this.load());
		System.import('script-loader!select2/dist/js/select2.min.js').then(()=>{
			
		})
	}
	ngOnDestroy(){
		
	}
	//初始化时间
	initDate(){
		var that = this;
		return new Promise(function (resolve, reject) {
			System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(
				()=>{
					System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
						$("#datetimepicker1").datetimepicker({//时间插件
							format: "yyyy-mm-dd",
						    language: "zh-CN",
						    autoclose: true,
						    minView : 2
						}).on("changeDate", function(e){
							var date = e.date;
							var time = common.getTimeDay(date);
							that.startTime=time;
							$("#datetimepicker2").datetimepicker("setStartDate", time);
						});
						$("#datetimepicker2").datetimepicker({//时间插件
							format: "yyyy-mm-dd",
						    language: "zh-CN",
						    autoclose: true,
						    minView : 2
						}).on("changeDate", function(e){
							var date = e.date;
							var time = common.getTimeDay(date);
							that.endTime=time;
							$("#datetimepicker1").datetimepicker("setEndDate", time);
						});
						resolve();
					});
				}
			);
		})
	}
	//查询
	table_search(){
		this.curPage=1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//初始化加载list数据
	load() {
		this.startTime =  $('#datetimepicker1').val();
		this.endTime =  $('#datetimepicker2').val();
		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		var postData={
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				companyId:this.companyId,
				startTime:this.startTime,
				endTime:this.endTime,
				name:common.stringTrim(this.railName),
				typeId:this.siteType
			}
		}
		let reqdata='gridPager='+JSON.stringify(postData);
		this.jsonApiService.http("markerDetail/markerdetailCount.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				var len=data.exhibitDatas.length;
				for(var i=0;i<len;i++){
					//nowAmount  添加当前在场车辆数量字段
					if(data.exhibitDatas[i].carNumber&&data.exhibitDatas[i].carNumber!==''){
						var nowAmount=data.exhibitDatas[i].carNumber.split(';').length;
						data.exhibitDatas[i].nowAmount=nowAmount;
					}
					else{
						data.exhibitDatas[i].nowAmount=0;
					}
					//载重量保留2位小数
					data.exhibitDatas[i].weight=common.twoDecimal(data.exhibitDatas[i].weight);
				}
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}
		})
	}
	//删除所选时间
	del_date(){
		this.startTime = '';
		this.endTime ='';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker2").val('').change();
		$("#datetimepicker2").datetimepicker("setStartDate", null);
		$("#datetimepicker1").datetimepicker("setEndDate", null);
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//点击车辆跳转明细
	jumpRailDetail(carNumber){
		localStorage.setItem('detailCar',carNumber);
		this.router.navigate(['/data-statistics/rail/rail-detail']);
	}
	//展开当前在场车辆弹框加载数据
	showNowCar(params,val){
		this.nowCarlist=[];
		var arr=params.carNumber.split(';');
		var len=arr.length;
		if(!params.carNumber||params.carNumber==''){
			this.jsonApiService.message('没有在场车辆');
			return;
		}
		for(var i=0;i<len;i++){
			var indexCar=arr[i].split(',');
			var car_number=indexCar[0];
			var status=indexCar[1];
			if(status=='1'){
				status='尚未装货';
			}
			if(status=='2'){
				status='开始装货';
			}
			if(status=='3'){
				status='装货结束';
			}
			this.nowCarlist[i]={carNumber:car_number,status:status}
		}
		val.show();
	}
	hideNowCar(val){
		val.hide();
	}
	//公司选中回调
	companySelected(data){
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.companyId = data.companyId;
	}
}
